<?php if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

class MY_Controller extends CI_Controller
{
    private $stylesheets;
    private $scriptfiles;
    protected $data_common;
    protected $logged_account;

    public function __construct()
    {
        parent::__construct();
        $this->load->library('session');
        $this->load->library('twig_wrapper', array('use_cache' => false));


        $this->load->helper('url');

        $this->config->load('baseinfo', TRUE);
        $this->stylesheets = array();
        $this->scriptfiles = array();

        $this->_common();

        $this->load->model('base_model');

    }

    protected function _common()
    {
        //公共样式表与脚本
        //TODO：线上环境就改用CDN
        $this->_add_style_sheet("Bootstrap", base_url("assets/bootstrap/css/bootstrap.min.css"));
        $this->_add_style_sheet("Font Awesome", "https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.5.0/css/font-awesome.min.css");
        $this->_add_style_sheet("Ionicons", "https://cdnjs.cloudflare.com/ajax/libs/ionicons/2.0.1/css/ionicons.min.css");

        $this->_add_script_file("jQuery 2.2.0", base_url("assets/jquery/jQuery-2.2.0.min.js"));
        $this->_add_script_file("Bootstrap", base_url("assets/bootstrap/js/bootstrap.min.js"));
        //$this->_add_script_file("jQuery 3.2.1", base_url("assets/jquery/jquery-3.2.1.min.js"));


        $this->data_common = array(
            'site_name' => $this->config->item('site_name', 'baseinfo'),
            'page_name' => $this->config->item('site_name', 'baseinfo'),
            'index_url' => '#'
        );
        $this->data_common['url'] = array(
            'index' => site_url(""),
            'admin' => site_url("admin"),
            'dateTable_i18n_cn' => base_url('assets/DataTables/js/jquery.dataTables-Chinese.json'),
            'login' => site_url("login"),
            'logout' => site_url("logout"),
        );
        $this->data_common['user'] = array(
            'name' => $this->session->uname ?: '未登录',
            'avatar_url' => base_url('assets/AdminLTE/img/user3-128x128.jpg'),
            'job_title' => 'none',
            'description' => '您还没有登录'
        );
    }

    public function index()
    {
        $data = array();
        $this->_view('framework.twig', $data);
    }

    public function auth()
    {
        $cookie_uid = $_COOKIE['uid'];
        $sess_uid = $this->session->uid;
        if (empty($sess_uid) or $sess_uid != $cookie_uid) {
            //重定向到登录页
            header("Location:" . site_url("login"));
            exit();
        }
    }

    //情境背景
    public function classesBg($classes, $text)
    {
        return '<div class="alert alert-' . $classes . ' alert-dismissible"><i class="icon fa fa-check"></i>' . $text . '</div>';
    }

    protected function _add_script_file($title, $src)
    {
        $script_file = array(
            'title' => $title,
            'src' => $src,
        );
        $this->scriptfiles[] = $script_file;
    }

    protected function _add_style_sheet($title, $href)
    {
        $script_file = array(
            'title' => $title,
            'href' => $href,
        );
        $this->stylesheets[] = $script_file;
    }

    protected function _view($template_name, $data_page, $class_page='', $router_page='')
    {
        //根据页面类型进行资源调度
        switch ($class_page){
            case 'admin': //后台页，将加载aminlte框架相关内容
                $this->_add_style_sheet("AdminLTE", base_url("assets/AdminLTE/css/AdminLTE.min.css"));
                $this->_add_style_sheet("AdminLTE Skin", base_url("assets/AdminLTE/css/skins/skin-blue.min.css"));
                $this->_add_script_file("AdminLTE", base_url("assets/AdminLTE/js/app.min.js"));

                $this->data_common['sidebar_menu'] = $this->get_adminlte_menu($router_page);
                break;
            default: //默认 加载bootstrap,nav,footer等资源
                $this->_add_style_sheet("Bootstrap", base_url("assets/vendor/twbs/bootstrap/dist/css/bootstrap.min.css"));
                $this->_add_style_sheet("Main", base_url("assets/css/main.css"));

                $this->_add_script_file("Bootstrap", base_url("assets/vendor/twbs/bootstrap/dist/js/bootstrap.min.js"));

                $this->data_common['nav_menu'] = $this->get_nav_menu();
        }

        //合并出$context
        $context = array_merge($this->data_common, $data_page);
        $context['stylesheets'] = $this->stylesheets;
        $context['scriptfiles'] = $this->scriptfiles;
        $this->twig_wrapper->parse($template_name, $context);
    }

    public function get_adminlte_menu($active)
    {
        $menus = json_decode(file_get_contents('./assets/config/sidebar_menu.json'), true);
        foreach ($menus as $key => $value) {
            if ($value['route'] == $active) {
                $menus[$key]['active'] = true;
            } else {
                $menus[$key]['active'] = false;
            }
            $menus[$key]['url'] = site_url($menus[$key]['route']);
        }
        return $menus;
    }
    public function get_nav_menu(){
        $menus = $this->base_model->get_navMenus('column');
        $navs = array();
        //print_r($menus);
        foreach ($menus as $key => $menu){
            //创建父菜单
            if($menu['parent'] == 0){
                $navs[$menu['id']] = array();
                array_push($navs[$menu['id']], $menu);
            }else{
                //print_r($navs);
                //添加子菜单
                array_push($navs[$menu['parent']], $menu);
            }
        }
        return $navs;
    }
}

/* End of file MY_Controller.php */
/* Location: ./application/libraries/MY_Controller.php */